/**
 * 推广链接功能支持
 */
var spcode = null;

//页面载入初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 键盘事件
	$( "body" ).bind( "keyup", bodyKeyupHandler );

	// 初始化
	button_bind();
} );

//--------------------------------------------------------------------------
//
//	方法
//
//--------------------------------------------------------------------------

function button_bind()
{
	// 提交保存
	$( "#btnSubmit" ).bind( "click", function()
	{
		submit_form();
	} );

	// 绑定下拉框关系
	$( "#sp_user" ).relate( $( "#sp_user_ipt" ) );
}

// 显示推广 链接
function set_sp_link( spcode,ask)
{
	var link = "";
	if(ask=='zice'){
		link = "http://www.taleu.com/zice.html?sp=" + spcode;
		//link = "http://www.taleu.com/zice.html";
	}else if(ask == 'wenjuan'){
		link = "http://www.taleu.com/wenjuan.html?sp=" + spcode;
		//link = "http://www.taleu.com/wenjuan.html";
	}else{
		link = "http://t.jingsiedu.com/t.php?sp=" + spcode;
	}
	$( "#sp_link_container" ).html( "<a id='sp_link'>" + link + "</a>" );
	$( "#sp_link" ).click( sp_link_copy );
}

function sp_link_copy( e )
{
}

function set_init_data( d )
{
	if( !empty( d ) && !empty( d.spcode ) )
	{
		spcode = d.spcode;

		$( "#sp_name" ).val( d.name );
		$( "#sp_ask" ).val( d.ask );
		$( "#sp_source" ).find( ":radio[value='" + d.source + "']" ).prop( "checked", true );
		$( "#sp_user" ).val( d.username );
		set_sp_link( d.spcode );
	}
	else
	{
		spcode = null;

		$( "#sp_name" ).val( "" );
		$( "#sp_ask" ).val( "" );
		$( "#sp_source" ).find( ":radio" ).prop( "checked", false );
		$( "#sp_user" ).val( "" );
		$( "#sp_link_container" ).empty();
	}
}

//--------------------------------------------------------------------------
//
//	数据提交
//
//--------------------------------------------------------------------------

function submit_form()
{
	var data = {};

	// 待更新的数据
	data.spcode = $("#spcode").val();

	// 组织提交数据
	data.name = $( "#sp_name" ).val();
	data.ask = $( "#sp_ask" ).val();
	data.source = $( "#sp_source" ).find( "input:checked" ).val();
	data.user = $( "#sp_username" ).val();

	submitter.post( "spread_add_go.json", data, function( d )
	{
		spcode = d.spcode;
		var ask = d.ask;
		set_sp_link( spcode,ask);
		
	}, "json" );
}

//--------------------------------------------------------------------------
//
//	事件
//
//--------------------------------------------------------------------------

function bodyKeyupHandler( e )
{
	if( e.ctrlKey && e.which == 13 )
	{
		submit_form();
	}
}
